package com.java.web;

import com.java.dao.commDao;
import com.java.dao.commDaoImpl;
import com.java.doMain.PageBean;
import com.java.doMain.commodity;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author xyd
 * @Date 2021/12/22 20:24
 * @Version 1.0
 */
public class typeServlet extends HttpServlet {

    private commDao commDao = new commDaoImpl();

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //默认从第一条开始查
        int currentPage = 0;
        //接收分类信息
        String category = req.getParameter("category");
        //接收当前页码 为了防止是null的所以抛出空指针
        String c = "";
        int i = 0;
        try {
            c = req.getParameter("currentPage");
        } catch (Exception e) {

        }
        //如果是null的 那就把临时的i赋值为0
        if (c == null){
            currentPage = 1;
        }else {
            //如果不是null就把传递过来的页码赋值给i
           i = Integer.parseInt(c);
            //然后把i赋给当前页码
            currentPage = i;
        }

        //将一些参数封装到page类里方便一会回传给页面
        PageBean p = new PageBean();
        p.setCurrentPage(currentPage);
        //查询该分类下的数据
        List<commodity> commodities = commDao.selectByType(category, currentPage);
        p.setPs(commodities);
        p.setCategory(category);
        Integer integer = commDao.selectByAll(category);
        p.setTotalCount(integer);
        p.setTotalPage((integer/4)+1);
        req.setAttribute("type",p);


        req.getRequestDispatcher(req.getContextPath() + "/client/product_list.jsp").forward(req,resp);
    }
}
